<?php include("session.inc.php"); ?>
<?php require_once("../common/config.inc.php");?>
<?php include("../common/db.inc.php"); ?>
<?php
global $currentJournal,$divDisplayStyle,$operateResult,$currentDate;

	init();		
	
	function init(){
		$GLOBALS[divDisplayStyle]=array();
		$operation=$_POST["operation"];
		$GLOBALS[currentDate]=date("Y-m-d H:i:s",time());
		//handle post request - create Journal 
		if($operation && $operation=="CreateJournal"){
			$GLOBALS[divDisplayStyle]["operateDiv"]="display:none";
			$GLOBALS[divDisplayStyle]["operateResultDiv"]="display:inline";
			//echo "execute create user...<br/>";
			createJournal();
			$GLOBALS[operateResult]="Create journal succeed!";
		}else{
			$GLOBALS[divDisplayStyle]["operateDiv"]="display:inline";
			$GLOBALS[divDisplayStyle]["operateResultDiv"]="display:none";	
		}	
	}
	
	function createJournal(){
		$name=$_POST["name"];
		$facepic=$_POST["facepic"];
		$storename=$_POST["storename"];
		$originalname=$_POST["originalname"];
		$createdate=$_POST["createdate"];
		if($createdate==""){
			$createdate=date("Y-m-d H:i:s",time());
		}
		$brief=$_POST["brief"];			
		
		$sql = "insert into medcdb.t_journal (name,facepic,storename,originalname,createdate,brief)" .
				"values('$name','$facepic','$storename','$originalname','$createdate','$brief')";
		//echo "createUser sql is :".$sql;			
		$connection=getMEDCConnection();
		executeUpdate($sql,$connection);
	}	
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="" />
<meta name="Description" content="" />
<meta name="author" content="" />
<title>Journal Create Page</title>
<link rel="stylesheet" type="text/css" href="../css/medc.css" />
<script language="JavaScript" type="text/javaScript" src="../js/medc_common.js"></script>
<script language="JavaScript" type="text/javaScript" src="../js/CalendarPopup.js"></script>
<script language="JavaScript" type="text/javascript">	
	var CreateDateCalendar=null;
	
	function init(){
	    //set the scheduleTabCalendar
	    CreateDateCalendar = new CalendarPopup("CreateDateCalendarDiv");
	    CreateDateCalendar.setCssPrefix("TEST");
	    CreateDateCalendar.setReturnFunction("calendarReturnFunction");
	}
		
	function calendarReturnFunction(y, m, d) {
	    if (window.CP_targetInput != null) {
	        var dt = new Date(y, m - 1, d, 0, 0, 0);
	        if (window.CP_calendarObject != null) {
	            window.CP_calendarObject.copyMonthNamesToWindow();
	        }
	        window.CP_targetInput.value = formatDate(dt, window.CP_dateFormat);
	    } else {
	        alert('Use setReturnFunction() to define which function will get the clicked results!');
	    }
	}
		
	function clickCalendar(calendarName){
		var inputName=null;
		var anchorName=null;
		var calendarObj=null;
		if(calendarName=="CreateDateCalendar"){
			inputName="createdate";
			anchorName="CreateDateCalendarImg";
			calendarObj=CreateDateCalendar;
			
		}
		var inputObj=document.getElementById(inputName);
		calendarObj.select(inputObj,anchorName,'yyyy-MM-dd');	
		return false;	
	}	
	
	function doPost(operation){
		if(operation=="saveUser"){
		    var isConfirm = window.confirm("Are you sure to save the Journal?");
		    if (isConfirm == false) {
		        return;
		    }				
		}
		if(validate()==false){
			return;
		}
		document.getElementById("operation").value="CreateJournal";
		//document.forms["medcform"].attributes["action"]="JournalCreate.php";
		document.forms["medcform"].submit();
	}
	
	function validate(){
		
		//check Journal id,name,facepic,storename,originalname
		//var journalId=document.getElementById("id").value;
		var journalName=document.getElementById("name").value;
		var facepic=document.getElementById("facepic").value;
		var storename=document.getElementById("storename").value;
		var originalname=document.getElementById("originalname").value;
		var checkList=new Array(
		["Journal Name",journalName],
		["Cover Image",facepic],
		["Journal Store Name",storename],
		["Journal Original Name",originalname]
		);
		for(var i=0;i<checkList.length;i++){
			if(isWhitespace(checkList[i][1])){
				alert(checkList[i][0]+ " can not be empty!");		
				return false;	
			}
		}
		return true;
	}
</script>
</head>
<body onload="init()">
	<!--Top area-->
	<div id="toparea">
		<?php include "../common/header.inc.php"?>
	</div>
	<div id="menu">
		<?php include "AdminMenu.inc.php";?>
	</div>	
	<!--Main area-->
	<div id="mainarea" style="margin-left:220px;">
		<form method="post" action="JournalCreate.php" name="medcform">
		<input type="hidden" name="operation" id="operation" />
		<div id="operateDiv" style="<? echo $GLOBALS[divDisplayStyle]["operateDiv"] ?>" >
			<table>
				<tr style="display:none">
					<td align="left"><label for="id">Journal Id</label></td>
					<td align="left"><input type="text" name="id" id="id" maxLength="30" size="20" readonly="true" value="<? echo $GLOBALS[currentJournal]['id']?>"/><td>
				</tr>
				<tr>
					<td align="left" style="width:120px;"><label for="name">Journal Name</label></td>
					<td align="left"><input type="text" name="name" id="name" maxLength="30" size="20" value="<? echo $GLOBALS[currentJournal]['name']?>"/><td>
				</tr>
				<tr>
					<td align="left"><label for="facepic">Cover Image</label></td>
					<td align="left"><input type="text" name="facepic" id="facepic" maxLength="100" size="40" value="<? echo $GLOBALS[currentJournal]['facepic']?>"/><td>
				</tr>	
				<tr>
					<td align="left"><label for="storename">Store Name</label></td>
					<td align="left"><input type="text" name="storename" id="storename" maxLength="100" size="40" value="<? echo $GLOBALS[currentJournal]['storename']?>"/><td>
				</tr>	
				<tr>
					<td align="left"><label for="originalname">Origninal Name</label></td>
					<td align="left"><input type="text" name="originalname" id="originalname" maxLength="30" size="20" value="<? echo $GLOBALS[currentJournal]['originalname']?>"/><td>
				</tr>			
				<tr>
					<td align="left"><label for="createdate">Create Date</label></td>
					<td align="left" nowrap="true">		
						<div id="CreateDateDiv"><input name="createdate" id="createdate" type="text" title="Create Date" value="<? echo $GLOBALS[currentDate]?>"/>  
							<div id="CreateDateCalendarDiv" style="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></div>
							<img src="../images/icon-select-date.gif" alt="select date icon" name="CreateDateCalendarImg" id="CreateDateCalendarImg" height="14" width="13"
							onclick="clickCalendar('CreateDateCalendar')" />
						</div>		
					</td>
				</tr>		
				<tr>
					<td align="left" valign="top" ><label for="brief">Brief</label></td>
					<td align="left"><textarea id="brief" name="brief" rows="3" ><? echo $GLOBALS[currentJournal]['brief']?></textarea><td>
				</tr>
				<tr>
					<TD>&nbsp;</TD>
					<td>
						<div align="right">						
							<input type="button" name="CreateJournal" value="Save" id="CreateJournal" onclick="doPost('saveJournal')" />
						</div>	
					</td>
				</tr>				
			</table>
		</div><!--end of modify user div -->
			<div id="operateResultDiv" style="<? echo $GLOBALS[divDisplayStyle]["operateResultDiv"] ?>" >
			<div align="center">
			<p> <?php echo $GLOBALS[operateResult]; ?></p> 
			<a href="JournalCreate.php">Continue to create journal</a><br/>
			</div>
		</div>
		<div align="center">
		</div>
		</form>
	</div><!--end of main area -->
	<div class="myclear"></div>
	<div id="footer">
		<?php include "../common/footer.inc.php";?>
	</div>
</body>
</html>
